﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    class KeyBinding
    {
        public string BaseKey;
        public string ShiftKey;

        public KeyBinding(string baseKey, string shiftKey)
        {
            this.BaseKey = baseKey;
            this.ShiftKey = shiftKey;
        }
    }

    static class KeyboardHelper
    {
        public static Dictionary<Keys, KeyBinding> KeyBindings = new Dictionary<Keys, KeyBinding>();

        private static void AddBinding(Keys key, string baseKey, string shiftKey)
        {
            KeyboardHelper.KeyBindings.Add(key, new KeyBinding(baseKey, shiftKey));
        }

        public static void InitBindings()
        {
            AddBinding(Keys.OemPipe, "\\", "|");
            AddBinding(Keys.OemBackslash, "\\", "|");
            AddBinding(Keys.OemOpenBrackets, "[", "{");
            AddBinding(Keys.OemCloseBrackets, "]", "}");
            AddBinding(Keys.OemSemicolon, ";", ":");
            AddBinding(Keys.OemPlus, "=", "+");
            //AddBinding(Keys.OemTilde, "`", "~");
            AddBinding(Keys.OemQuotes, "'", "\"");
            AddBinding(Keys.OemQuestion, "/", "?");
            AddBinding(Keys.OemComma, ",", "<");
            AddBinding(Keys.OemPeriod, ".", ">");
            AddBinding(Keys.Decimal, ".", "");
            AddBinding(Keys.OemMinus, "-", "_");
            AddBinding(Keys.Space, " ", "");
            AddBinding(Keys.Tab, "TAB", "");
            AddBinding(Keys.D1, "1", "!");
            AddBinding(Keys.D2, "2", "@");
            AddBinding(Keys.D3, "3", "#");
            AddBinding(Keys.D4, "4", "$");
            AddBinding(Keys.D5, "5", "%");
            AddBinding(Keys.D6, "6", "^");
            AddBinding(Keys.D7, "7", "&");
            AddBinding(Keys.D8, "8", "*");
            AddBinding(Keys.D9, "9", "(");
            AddBinding(Keys.D0, "0", ")");
            AddBinding(Keys.NumPad1, "1", "!");
            AddBinding(Keys.NumPad2, "2", "@");
            AddBinding(Keys.NumPad3, "3", "#");
            AddBinding(Keys.NumPad4, "4", "$");
            AddBinding(Keys.NumPad5, "5", "%");
            AddBinding(Keys.NumPad6, "6", "^");
            AddBinding(Keys.NumPad7, "7", "&");
            AddBinding(Keys.NumPad8, "8", "*");
            AddBinding(Keys.NumPad9, "9", "(");
            AddBinding(Keys.NumPad0, "0", ")");
            AddBinding(Keys.A, "a", "A");
            AddBinding(Keys.B, "b", "B");
            AddBinding(Keys.C, "c", "C");
            AddBinding(Keys.D, "d", "D");
            AddBinding(Keys.E, "e", "E");
            AddBinding(Keys.F, "f", "F");
            AddBinding(Keys.G, "g", "G");
            AddBinding(Keys.H, "h", "H");
            AddBinding(Keys.I, "i", "I");
            AddBinding(Keys.J, "j", "J");
            AddBinding(Keys.K, "k", "K");
            AddBinding(Keys.L, "l", "L");
            AddBinding(Keys.M, "m", "M");
            AddBinding(Keys.N, "n", "N");
            AddBinding(Keys.O, "o", "O");
            AddBinding(Keys.P, "p", "P");
            AddBinding(Keys.Q, "q", "Q");
            AddBinding(Keys.R, "r", "R");
            AddBinding(Keys.S, "s", "S");
            AddBinding(Keys.T, "t", "T");
            AddBinding(Keys.U, "u", "U");
            AddBinding(Keys.V, "v", "V");
            AddBinding(Keys.W, "w", "W");
            AddBinding(Keys.X, "x", "X");
            AddBinding(Keys.Y, "y", "Y");
            AddBinding(Keys.Z, "z", "Z");

            // special bindings
            AddBinding(Keys.Enter, "ENTER", "ENTER");
            AddBinding(Keys.Back, "BACKSPACE", "BACKSPACE");
        }

        public static string KeyToCharactor(Keys key, bool shift)
        {
            if (KeyboardHelper.KeyBindings.ContainsKey(key))
            {
                if (shift)
                {
                    return KeyboardHelper.KeyBindings[key].ShiftKey;
                }
                else
                {
                    return KeyboardHelper.KeyBindings[key].BaseKey;
                }
            }
            else
            {
                return "";
            }
        }

        public static bool IsCharactorKey(Keys key)
        {
            return KeyboardHelper.KeyBindings.ContainsKey(key);
        }
    }
}
